package co.yixiang.yshop.module.order.controller.app.afterorder;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.security.core.annotations.PreAuthenticated;
import co.yixiang.yshop.module.order.controller.app.afterorder.param.AppReturnLogisticsParam;
import co.yixiang.yshop.module.order.controller.app.afterorder.param.AppStoreAfterSalesParam;
import co.yixiang.yshop.module.order.controller.app.afterorder.vo.AppAfterSalesDetailQueryVo;
import co.yixiang.yshop.module.order.controller.app.afterorder.vo.AppStoreAfterSalesVo;
import co.yixiang.yshop.module.order.controller.app.afterorder.vo.AppStoreOrderAfterSalesDetailVo;
import co.yixiang.yshop.module.order.controller.app.afterorder.vo.AppStoreOrderCartInfoVo;
import co.yixiang.yshop.module.order.service.storeaftersales.AppStoreAfterSalesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @author : yshop 2023/6/23 16:00
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Tag(name = "用户 APP - 订单售后")
@RequestMapping("/after")
public class AppOrderAfterSalesController {

    private final AppStoreAfterSalesService appStoreAfterSalesService;

    @PreAuthenticated
    @PostMapping("/applyForAfterSales")
    @Operation(summary = "申请售后")
    public CommonResult<Long> applyForAfterSales(@Valid @RequestBody AppStoreAfterSalesParam storeAfterSalesParam) {
        Long uid = getLoginUserId();
        return success(appStoreAfterSalesService.applyForAfterSales(uid, storeAfterSalesParam));
    }

    @PreAuthenticated
    @PostMapping("/applyForAfterSales/get")
    @Operation(summary = "查询订单详情")
    @Parameter(name = "key", description = "唯一的uni值或者订单号", required = true, example = "10")
    public CommonResult<List<AppStoreOrderCartInfoVo>> checkOrderDetails(@RequestBody AppAfterSalesDetailQueryVo vo) {
        return success(appStoreAfterSalesService.checkOrderDetails(vo));
    }

    @PreAuthenticated
    @GetMapping("/checkForAfterSales/{key}")
    @Operation(summary = "查询售后订单详情")
    @Parameter(name = "key", description = "唯一的uni值或者售后单号", required = true, example = "10")
    public CommonResult<AppStoreOrderAfterSalesDetailVo> checkAfterSalesDetails(@PathVariable String key) {
        return success(appStoreAfterSalesService.checkAfterSalesDetails(key));
    }


    @PreAuthenticated
    @GetMapping("/storeAfterSales/list")
    @Operation(summary = "售后列表")
    @Parameters({
            @Parameter(name = "type", description = "商品状态,-1全部 1售后中 2 已完成",
                    required = true, example = "1"),
            @Parameter(name = "page", description = "页码,默认为1",
                    required = true, example = "1"),
            @Parameter(name = "limit", description = "页大小,默认为10",
                    required = true, example = "10")
    })
    public CommonResult<List<AppStoreAfterSalesVo>> salesList(@RequestParam(value = "type", defaultValue = "0") int type,
                                                              @RequestParam(value = "page", defaultValue = "1") int page,
                                                              @RequestParam(value = "limit", defaultValue = "10") int limit) {
        Long uid = getLoginUserId();
        return success(appStoreAfterSalesService.salesList(uid, type, page, null, limit));
    }


    @PreAuthenticated
    @GetMapping("/store/detail/{key}/{id}")
    @Parameters({
            @Parameter(name = "key", description = "唯一的uni值或者订单号", required = true, example = "10      "),
            @Parameter(name = "id", description = "售后id", required = true, example = "1")
    })
    @Operation(summary = "查看售後详情")
    public CommonResult<AppStoreAfterSalesVo> detail(@PathVariable String key, @PathVariable Long id) {
        Long uid = getLoginUserId();
        AppStoreAfterSalesVo storeInfo = appStoreAfterSalesService.getStoreInfoByOrderCodeAndAfterIdAndUid(key, id, uid);
        return success(storeInfo);
    }

    @PreAuthenticated
    @GetMapping("/store/detail/{key}")
    @Parameter(name = "key", description = "唯一的uni值或者订单号", required = true, example = "10      ")
    @Operation(summary = "通过订单号搜索")
    public CommonResult<List<AppStoreAfterSalesVo>> detail(@PathVariable String key) {
        Long uid = getLoginUserId();
        return success(appStoreAfterSalesService.getStoreInfoByOrderCodeAndUid(key, uid));
    }

    @PreAuthenticated
    @GetMapping("/revoke/{key}/{id}")
    @Parameter(name = "key", description = "唯一的uni值或者订单号", required = true, example = "10      ")
    @Operation(summary = "撤销申请")
    public CommonResult<Boolean> revoke(@PathVariable String key, @PathVariable Long id) {
        Long uid = getLoginUserId();
        Boolean revoke = appStoreAfterSalesService.revoke(key, uid, id);
        return success(revoke);
    }


    @PreAuthenticated
    @PostMapping("/addLogisticsInformation")
    @Operation(summary = "添加物流信息")
    public CommonResult<Boolean> addLogisticsInformation(@RequestBody AppReturnLogisticsParam param) {
        Boolean result = appStoreAfterSalesService.addLogisticsInformation(param);
        return success(result);
    }


    @PreAuthenticated
    @DeleteMapping("/deleteAfterSalesOrder")
    @Parameters({
            @Parameter(name = "orderCode", description = "订单编号", required = true),
            @Parameter(name = "id", description = "售后订单id", required = true, example = "1")
    })
    @Operation(summary = "删除售后订单")
    public CommonResult<Boolean> deleteAfterSalesOrder(@RequestParam String orderCode, @RequestParam Long id) {
        Boolean result = appStoreAfterSalesService.deleteAfterSalesOrder(orderCode, id);
        return success(result);
    }


}
